package com.nova.bluetooth.base;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.nova.bluetooth.helpUtils.GlideUtil;

import java.util.List;

public class CommonBindingAdapter {

    @BindingAdapter(value = {"setText"})
    public static void setText(TextView textView, String text) {
        if(!TextUtils.isEmpty(text)){
            textView.setText(text);
        }
    }

    @BindingAdapter(value = {"setImageRes"})
    public static void setImageRes(ImageView imageView, int imageRes) {
        imageView.setImageResource(imageRes);
    }

    @BindingAdapter(value = {"loadImg"})
    public static void loadImg(ImageView imageView, String loadUrl) {
        if(!TextUtils.isEmpty(loadUrl)){
            GlideUtil.loadImg(imageView.getContext(), loadUrl, imageView);
        }
    }

    @BindingAdapter(value = {"loadRoundedImg","roundingRadius"})
    public static void loadRoundedImg(ImageView imageView, String loadUrl, int roundingRadius) {
        if(!TextUtils.isEmpty(loadUrl)){
            GlideUtil.loadRoundedImg(imageView, loadUrl, imageView,roundingRadius);
        }
    }


    @BindingAdapter(value = {"setSelected"})
    public static void setSelected(View view, boolean isSelected) {
        view.setSelected(isSelected);
    }

    @BindingAdapter(value = {"adapter"})
    public static void adapter(RecyclerView recyclerView, BaseQuickAdapter baseQuickAdapter) {
        recyclerView.setAdapter(baseQuickAdapter);
    }

    @BindingAdapter(value = {"initLayoutManager", "initAdapter"})
    public static void initRv(RecyclerView recyclerView, RecyclerView.LayoutManager layoutManager,
                              BaseQuickAdapter mAdapter) {
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(mAdapter);
    }

    @BindingAdapter(value = {"setAdapter", "setNewData"})
    public static void setNewData(RecyclerView recyclerView, BaseQuickAdapter mAdapter, List dataResult) {
        if (mAdapter != null) {
            if (dataResult != null) {
                mAdapter.setList(dataResult);
            }
        }
    }

    @BindingAdapter(value = {"addItemDecoration"})
    public static void addItemDecoration(RecyclerView recyclerView, RecyclerView.ItemDecoration itemDecoration) {
        recyclerView.addItemDecoration(itemDecoration);
    }

    @BindingAdapter(value = {"setVisibility"})
    public static void setVisibility(View view, int visibility) {
        view.setVisibility(visibility);
    }

    @BindingAdapter(value = {"setGone"})
    public static void setGone(View view, boolean isGone) {
        view.setVisibility(isGone ? View.GONE : View.VISIBLE);
    }

    @BindingAdapter(value = {"setPhone"})
    public static void setPhone(TextView textView, String phone) {
        String phoneStart = phone.substring(0, 3);
        String phoneEnd = phone.substring(phone.length() - 2);
        String resultPhone = phoneStart + "******" + phoneEnd;
        textView.setText(resultPhone);
    }

    @BindingAdapter(value = "setOnTouchListener")
    public static void setOnTouchListener(View view, View.OnTouchListener onTouchListener) {
        view.setOnTouchListener(onTouchListener);
    }

    @BindingAdapter(value = "setDoubleText")
    public static void setDoubleText(TextView textView, Double text) {
        textView.setText(String.valueOf(text));
    }

    @BindingAdapter(value = "setIntegerText")
    public static void setIntegerText(TextView textView, Integer text) {
        textView.setText(String.valueOf(text));
    }

    @BindingAdapter(value = "setFloatText")
    public static void setFloatText(TextView textView, Float text) {
        textView.setText(String.valueOf(text));
    }

    @BindingAdapter(value = "setTextColor")
    public static void setTextColor(TextView textView, int color) {
        textView.setTextColor(color);
    }

    @BindingAdapter(value = "setBackgroundResource")
    public static void setBackgroundResource(View view, int resid){
        view.setBackgroundResource(resid);
    }
    @BindingAdapter(value = "setEmptyTxtVisibility")
    public static void setEmptyTxtVisibility(TextView textView, String text) {
        if(TextUtils.isEmpty(text)){
            textView.setVisibility(View.GONE);
        }else {
            textView.setVisibility(View.VISIBLE);
        }
    }

//
//    @BindingAdapter(value = {"setOnRefreshLoadMoreListener"})
//    public static void setOnRefreshLoadMoreListener(DidoSmartRefreshLayout commonSrl, OnRefreshLoadMoreListener onRefreshLoadMoreListener) {
//        commonSrl.setOnRefreshLoadMoreListener(onRefreshLoadMoreListener);
//    }
//
//    @BindingAdapter(value = {"isAutoRefreshAnim"})
//    public static void isAutoRefreshAnim(DidoSmartRefreshLayout commonSrl, boolean isAutoRefreshAnim) {
//        if (isAutoRefreshAnim) {
//            commonSrl.autoRefresh();
//        }
//    }
//
//    @BindingAdapter(value = {"accentColor"})
//    public static void setAccentColor(DidoClassicsHeader classicsHeader, int accentColor) {
//        classicsHeader.setAccentColor(accentColor);
//    }
//
//    @BindingAdapter(value = {"accentColor"})
//    public static void setAccentColor(DidoClassicsFooter classicsFooter, int accentColor) {
//        classicsFooter.setAccentColor(accentColor);
//    }
//
//    @BindingAdapter(value = {"notifyRefreshStatus"})
//    public static void notifyRefreshStatus(DidoSmartRefreshLayout commonSrl, BaseDidoViewModel.RefreshStatus refreshStatus) {
//        if (refreshStatus.isFinishing) {
//            if (refreshStatus.isSuceess) {
//                if (refreshStatus.isHeader) {
//                    commonSrl.finishRefresh();
//                } else if (refreshStatus.isFooter) {
//                    commonSrl.finishLoadMore();
//                }
//            } else {
//                if (refreshStatus.isHeader) {
//                    commonSrl.finishRefresh(false);
//                } else if (refreshStatus.isFooter) {
//                    commonSrl.finishRefresh(false);
//                }
//            }
//        }
//    }
//
//    @BindingAdapter(value = {"setSrlEnableLoadMore"})
//    public static void setSrlEnableLoadMore(DidoSmartRefreshLayout commonSrl, boolean enableLoadMore) {
//        commonSrl.setEnableLoadMore(enableLoadMore);
//    }
//
//    @BindingAdapter(value = {"setEnableRefresh"})
//    public static void setEnableRefresh(DidoSmartRefreshLayout commonSrl, boolean enableRefresh) {
//        commonSrl.setEnableRefresh(enableRefresh);
//    }
//
//    @BindingAdapter(value = {"isNoMoreData"})
//    public static void isNoMoreData(DidoSmartRefreshLayout commonSrl, boolean isNoMoreData) {
//        KLog.d("isNoMoreData = " +isNoMoreData);
//        commonSrl.setNoMoreData(isNoMoreData);
//    }

    @BindingAdapter(value = {"loadingBgColor"})
    public static void setLoadingBackgroundColor(View view, int loadingBgColor) {
        view.setBackgroundColor(loadingBgColor);
    }

    @BindingAdapter(value = {"setSeekBarProgress"})
    public static void setSeekBarProgress(SeekBar view, int progress) {
        view.setProgress(progress);
    }

    @BindingAdapter(value = {"setSeekBarMax"})
    public static void setSeekBarMax(SeekBar view, int max) {
        view.setMax(max);
    }


    @BindingAdapter(value = {"setLayoutVisibility"})
    public static void setLayoutVisibility(View view, boolean isVisible) {
        if(isVisible){
            view.setVisibility(View.VISIBLE);
        }else {
            view.setVisibility(View.GONE);
        }
    }

    @BindingAdapter(value = {"setIsGone"})
    public static void setIsGone(View view, boolean gone) {
        view.setVisibility(gone ? View.GONE : View.VISIBLE);
    }

}
